<?php
class User {
	private $user_id;
	private $username;
  	
//Người tạo : Lại Ngọc Thái Châu - Team 0
//Module 	: manage login- logout-permission
//Chức năng	: quan ly viec login,logout,phan quyen
	
	public function init(){
		$session = new Zend_Session_Namespace('login.auth');
		$user = new  Admin_Model_Users();
		$groupuser = new  Admin_Model_GroupUser();
		
		if (isset($session->id_user)) {
			
			$user_query = $user->selectUserByUserID($session->id_user);
			
			if (count($user_query)>0) {
				$this->user_id = $user_query[0]['user_id'];
				$this->username = $user_query[0]['user_name'];
				
				$user_group_query= $groupuser->selectUserGroup($user_query[0]["group_id"]);
				$permissions = unserialize($user_group_query[0]['action']);
				if (is_array($permissions)) {
	  				foreach ($permissions as $key => $value) {
	    				$session->permission[$key] = $value;
	  				}
				}
				
			} else {
				$this->logout();
			}
    	}

  	}
  	// xu ly login
  	public function login($username, $password) {
  		$session = new Zend_Session_Namespace('login.auth');
    	$user = new  Admin_Model_Users();
		$groupuser = new  Admin_Model_GroupUser();
  		$user_query = $user->selectUserByUserNamePassWord($username,md5($password));
  		
    	if (count($user_query)>0) {
			$session->id_user = $user_query[0]['user_id'];
			$session->name_user = $user_query[0]['user_name'];	
			$this->user_id = $user_query[0]['user_id'];
			$this->username = $user_query[0]['user_name'];			

      		$user_group_query= $groupuser->selectUserGroup($user_query[0]["group_id"]);

	  		$permissions = unserialize($user_group_query[0]['action']);
			
			if (is_array($permissions)) {
				foreach ($permissions as $key => $value) {
					$session->permission[$key] =$value;
				}
			}
      		return true;
    	} else {
      		return false;
    	}
  	}
	// logout
  	public function logout() {
  		$session = new Zend_Session_Namespace('login.auth');
		unset($session->id_user);
		unset($session->permission);
		unset($session->name_user);
	
		$this->user_id = '';
		$this->username = '';
		
  	}
	// ham kiem tra
  	public function hasPermission($key, $value) {
   		$session = new Zend_Session_Namespace('login.auth');
  		if (isset($session->permission[$key])) {
	  		return in_array($value, $session->permission[$key]);
		} else {
	  		return false;
		}
  	}
  	/*xu ly kiem tra page back end co dc phep truy cap boi user hien hanh ko*/
	public function permission() {
			$route = '';
			$uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
			if ($uri!="") {
				
				$part = explode('/', $uri);
				
				if (isset($part[1])) {
					$route .= $part[1];
				}
				
				if (isset($part[2])) {
					$route .= '/' . $part[2];
				}
			}
			$ignore = array(
				'admin/installwidget',
				'admin/uninstallwidget',
				'admin/error',
				'admin/login',
			
			);		
			if($route=="admin")	
			{
				$route = "admin/index";
				header("location: /admin/index");	
			}
			if (!in_array($route, $ignore) && !$this->hasPermission('access', $route)) {
				{
					header("location: /admin/error");	
				}
		}
		return true;
	}
	/*xu ly kiem tra page back end co dc phep chinh sua boi user hien hanh ko*/
	public function modifiy($route) {
		if($route==null)
		{
			$route = '';
			$uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
			if ($uri!="") 
			{
				$part = explode('/', $uri);
				if (isset($part[1])) 
				{
					$route .= $part[1];
				}
				
				if (isset($part[2])) 
				{
					$route .= '/' . $part[2];
				}
			}
		}
		$ignore = array(
			'admin/installwidget',
			'admin/uninstallwidget',
			'admin/error',
			'admin/login'
			);		
				
			if (!in_array($route, $ignore) && !$this->hasPermission('modify', $route)) {
				 return false;
		}
		return true;
	}
	/*xu ly kiem tra widget co dc phep chinh sua boi user hien hanh ko*/
	public function modifiy_widget($route) {
		if($route!=null)
		{
			if (!$this->hasPermission('modify_widget', $route)) {
				 return "false";
			}
			return "true";
		}
		return "false";
	}
	/*xu ly kiem tra content widget co dc phep chinh sua boi user hien hanh ko*/
	public function modifiy_content($route) {
		if($route!=null)
		{
			if (!$this->hasPermission('modify_content', $route)) {
				 return "false";
			}
			return "true";
		}
		return "false";
	}

	// kiem tra login
	public function check_login() {
		$session = new Zend_Session_Namespace('login.auth');
		$user = new User();
		$route = '';
		$uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
		if ($uri!="") {
		
			$part = explode('/', $uri);
				
			if (isset($part[1])) {
				$route .= $part[1];
			}
				
			if (isset($part[2])) {
				$route .= '/' . $part[2];
			}
		}

		$ignore = array(
			
		);	
		
		if (!isset($session->id_user) && !in_array($route, $ignore)) {
			header("location: /admin/login");
		}
	return true;
	}
  		
}
?>